package com.fc.backingbeans.food;

import javax.faces.bean.ViewScoped

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

import com.fc.backingbeans.AbstractDialog
import com.fc.entities.Food
import com.fc.service.FoodGroupService
import com.fc.service.FoodService
import com.fc.service.UserDetailsService
import com.fc.utils.MessageUtils

/**
 * 
 * @author Ács Ádám
 *
 */
@Component("addFoodDialog")
@ViewScoped
class AddFoodDialog  extends AbstractDialog {
	@Autowired private FoodService foodService
	@Autowired private FoodGroupService foodGroupService
	@Autowired private UserDetailsService userDetailsService
	Food food = new Food()

	Food getFood() {
		if (food == null) {
			food = new Food();
		}
		return food;
	}

	void saveFood() {
		if (food) {
			food.user = userDetailsService.currentUser
			try {
				foodService.create(food)
				closeSilently()
			}
			catch (Exception ex) {
				errorState = true
				MessageUtils.addError("Az étel létrehozása nem lehetséges", "")
			}
		}
	}

	void updateFood() {
		if (food) {
			try {
				foodService.save(food)
				closeSilently()
			}
			catch (Exception ex) {
				errorState = true
				MessageUtils.addError("Az étel módosítása nem lehetséges", "")
			}
		}
	}

	void deleteFood() {
		if (food) {
			try {
				foodService.delete(food)
				closeSilently()
			} 
			catch (Exception ex) {
				MessageUtils.addError("Az étel törlése nem lehetséges", "")
			}
		}
	}

	@Override
	String getDialogName() {
		"fc/food/addFood"
	}

	@Override
	Map getOptions() {
		[
			modal: true
			,draggable: false
			,resizable: false
			,contentHeight: 470
			,contentWidth: 620
			,width: 650
			,height: 500
		]
	}
}
